// pages/user/setting/set-newpwd/index.js
import { requestLoading} from '../../../../utils/network.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    oldPassword: '',// 旧密码
    newPassword: '', //新密码
    repassword: '' // 重复密码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  inputNewPassword:function(e) {
    let password = e.detail.value;
    let that = this;
    if (password.length < 6){
      wx.showToast({
        title: '密码不少于6位',
        icon: 'none'
      })
      return false;
    } else {
      that.setData({
        newPassword: password
      })
    }
  },
  inputRePassword: function(e) {
    let repass = e.detail.value;
    let that = this
    if (repass !== that.data.password){
      wx.showToast({
        title: '两次密码不一致',
        icon: 'none'
      })
      return false
    } else {
      that.setData({
        repassword: repass
      })
      // wx.redirectTo({
      //   url: '/pages/login/login',
      // })
    }
  },
  // 提交更改密码
  formSubmit (e) {
    let data = e.detail.value
    let that = this
    requestLoading(
      '/distributor/updatePassword',
      data,
      '正在修改',
      function success (res) {
        let code = res.resultCode
        if (code === 2000) {
          wx.showToast({
            title: '密码修改成功',
            icon: 'success',
            duration: 2000
          })
          wx.removeStorage({
            key: 'token',
            success: function (res) {
              wx.redirectTo({
                url: '/pages/login/login',
              })
            },
          })
        }
      },
      function fail(){
        wx.showToast({
          title: '加载数据失败',
          icon: 'none'
        })
      }
    )
  } 
})